<?php
namespace app\agent\controller;

use think\Db;
use think\Request;

class Salesmantype extends  Base
{
    public function index(){
        $get['name']=isset($_GET['name'])?$_GET['name']:'';
        $get['p']=isset($get['p'])?$get['p']:config('paginate.list_rows');
        if(empty($_GET)){
            $list = Db::name('salesman_type')
                ->where(['aid'=>session('agents.id'),'vid'=>3])
                ->where('state','not in',3)
                ->order('id desc')
                ->paginate(10);
        }else{
            $list = Db::name('salesman_type')
                ->where(['aid'=>session('agents.id'),'vid'=>3])
                ->where('state','not in',3)
                ->where('typename','like',"{$get['name']}%")
                ->order('id desc')
                ->paginate(10);
        }
        $page = $list->render();
        return view('salesmantype/index',['list'=>$list,'page'=>$page,'get'=>$get]);

    }

    public function add(){
        if(empty($_POST)){
            return view('salesmantype/add');
        }else{
            $param = myarr($this->request->post());
            $aid = session('agents.id');
            $type = Db::name('salesman_type')
                ->where(['aid'=>$aid,'typename'=>$param['typename'],'vid'=>3])
                ->where('state','not in',3)
                ->find();
            if($type) return "<script>alert('添加失败，该部门名称已经存在');history.go(-1);</script>";
            $data = [
                'aid' => session('agents.id'),
                'vid' => 3,
                'typename'=>$param['typename'],
                'createtime'=>date('Y-m-d H:i:s'),
                'state'=>$param['state'],
            ];
            $in = Db::name('salesman_type')->insertGetId($data);

            $datas = [
                'wid'=>session('agents.id'),
                'logintoip' => $_SERVER['REMOTE_ADDR'],
                'logintime' => date('Y-m-d H:i:s'),
                'operation' => '新增业务员的部门ID:'.$in.'/名称:'.$param['typename'],
                'va'=>2,//代理商
            ];
            Db::name('login_va')->insert($datas);

            if($in){
                return "<script>alert('添加成功');parent.location.reload();</script>";
            }
        }
    }

    public function state(){
        $aid = session('agents.id');
        $type = Db::name('salesman_type')->where(['id'=>$_GET['id'],'aid'=>$aid,'vid'=>3])->find();
        if($type['state']==1){

            $up = Db::name('salesman_type')->where('id',$type['id'])->update(['state'=>2]);
            if($up){
                $data = [
                    'wid'=>session('agents.id'),
                    'logintoip' => $_SERVER['REMOTE_ADDR'],
                    'logintime' => date('Y-m-d H:i:s'),
                    'operation' => '关闭ID为:'.$type['id'].'的业务员部门',
                    'va'=>2,//代理商
                ];
                Db::name('login_va')->insert($data);
                return "<script>window.location.href = document.referrer;</script>";
            }
        }elseif($type['state']==2){
            $up = Db::name('salesman_type')->where('id',$type['id'])->update(['state'=>1]);
            if($up){
                $data = [
                    'wid'=>session('agents.id'),
                    'logintoip' => $_SERVER['REMOTE_ADDR'],
                    'logintime' => date('Y-m-d H:i:s'),
                    'operation' => '开启ID为:'.$type['id'].'的业务员部门',
                    'va'=>2,//代理商
                ];
                Db::name('login_va')->insert($data);
                return "<script>window.location.href = document.referrer;</script>";
            }
        }
    }

    public function edit(){
        if(empty($_POST)){
            $type = Db::name('salesman_type')->where(['aid'=>session('agents.id'),'id'=>$_GET['id']])->find();
            return view('salesmantype/edit',['type'=>$type]);
        }else{
            $param = myarr($this->request->post());
            $data = [
                'typename'=>$param['typename'],
                'state'=>$param['state'],
            ];
            $up = Db::name('salesman_type')->where('id',$param['id'])->update($data);
            if($up){
                $data = [
                    'wid'=>session('agents.id'),
                    'logintoip' => $_SERVER['REMOTE_ADDR'],
                    'logintime' => date('Y-m-d H:i:s'),
                    'operation' => '修改ID为:'.$param['id'].'业务员部门信息',
                    'va'=>2,//代理商
                ];
                Db::name('login_va')->insert($data);
                return "<script>alert('修改成功');parent.location.reload();</script>";
            }else{
                return "<script>alert('修改未成功');parent.location.reload();</script>";
            }
        }
    }

//    public function del(){
////        $sa = Db::name('salesman')->where('')
////        dump($_GET);die;
//        $sa = Db::name('salesman')->where(['aid'=>session('agents.id'),'post'=>$_GET['id']])->find();
//        if($sa) return "<script>alert('删除失败，该请先删除该部门下的所有业务员');parent.location.reload();</script>";
//        $del = Db::name('salesman_type')->where(['aid'=>session('agents.id'),'id'=>$_GET['id']])->update(['state'=>3]);
//        if($del){
//            return "<script>alert('删除成功');history.go(-1);</script>";
//        }
//    }
}
